{% extends 'layout/manage.html' %}
{% block css %}
    {% load static %}
    <link rel="stylesheet" href="{% static 'plugin/editor.md-master/css/editormd.min.css' %}">
    <style>
        .panel-body {
            padding: 0;
        }

        .panel-heading .function {
            display: flex !important;
            flex-direction: row !important;
            justify-content: space-between !important;
        }

        .title-list {
            border-right: 1px solid #dddddd;
            min-height: 500px;

        }

        .content {
            border-left: 1px solid #dddddd;
            min-height: 600px;
            margin-left: -1px;
        }
        .title-list ul {
            padding-left: 15px;

        }

        .title-list li {
            list-style: none;
        }

        .title-list ul a {
            display: block;
            padding: 5px 0;
        }
    </style>
{% endblock %}
{% block content %}
    <div class="container-fluid">
        <div class="panel panel-primary">
            <div class="panel-heading"
                 style="display: flex; flex-direction: row; justify-content: space-between">
                <div>
                    <i class="fa fa-book" aria-hidden="true"></i>
                    wiki知识库
                </div>
                <div class="function"
                     style="display: flex; flex-direction: row; justify-content: space-between">
                    <a href="{% url 'wiki_add' request.obj.0.0 %}" type="button" class="btn btn-success btn-xs">
                        新建 <i class="fa fa-plus-circle" aria-hidden="true"></i>
                    </a>
                </div>
            </div>
            <div class="panel-body">
                <div class="col-sm-3 title-list">
                    <ul id="catalog">

                    </ul>

                </div>
                <div class="col-sm-9 content">
                    <div style="text-align: center; margin-top: 50px">
                        <form method="post">
                            {% csrf_token %}
                            {% for i in form %}
                                {% if i.name == 'content' %}
                                    <div class="form-group">
                                    <label for="{{ i.id_for_label }}">{{ i.label }}</label>
                                        <div id="editor">
                                            {{ i }}
                                        </div>
                                    <span class="errors"></span>
                                </div>
                                    {% else %}
                                    <div class="form-group">
                                    <label for="{{ i.id_for_label }}">{{ i.label }}</label>
                                    {{ i }}
                                    <span class="errors"></span>
                                </div>
                                {% endif %}

                            {% endfor %}


                            <button type="submit" class="btn btn-default">Submit</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <input type="text" value="{{ text }}" style="display: none" id="trval">
{% endblock %}


{% block js %}

    <script src="{% static 'plugin/editor.md-master/editormd.min.js' %}"></script>
    <script>

        var WIKI_DETAIL_URL = '{% url 'wiki' project_id=request.obj.0.0 %}';
        {% if text %}
        var texted = $('#trval').val();
        $('[name="content"]').text(texted);
        {% endif %}
        $(function () {
            initCatalog();
            // 初始号了markdown编辑器 (textarea转化为编辑器)
            initEdtorMd();
        });
        function initCatalog() {
            $.ajax({
                url: '{% url 'wiki_catalog' project_id=request.obj.0.0 %}',
                type: 'get',
                dataType: 'JSON',
                success: function (res) {
                    console.log(res);
                    if (res.status) {
                        $.each(res.ret, function (index, item) {
                            if (item[2] == null) {
                                var href = WIKI_DETAIL_URL + '?wiki_id=' + item[0];
                                var li = $('<li>').attr('id', 'id_' + item[0]).append($('<a>').text(item[1]).attr('href', href)).append($('<ul>'));
                                $('#catalog').append(li)
                            } else {
                                var href = WIKI_DETAIL_URL + '?wiki_id=' + item[0];
                                var lis = $('<li>').attr('id', 'id_' + item[0]).append($('<a>').text(item[1]).attr('href', href));
                                $('#id_' + item[2]).children('ul').append(lis)
                            }
                        })
                    }
                }
            })
        }
        // 初始化edotor
        function initEdtorMd() {
            editormd('editor', {
                placeholder: "请输入内容",
                height: 500,
                path:'{% static 'plugin/editor.md-master/lib/' %}',
                imageUpload:true,
                imageFormats:['jpg', 'png', 'gif', 'jpeg'],
                imageUploadURL:'{% url 'wiki_upload' project_id=request.obj.0.0%}'
            })
        }
    </script>


{% endblock %}